﻿// register your Gadget's namespace
registerNamespace("Microsoft.Live.GadgetSDK");

// define the constructor for your Gadget (this must match the name in the manifest xml)
Microsoft.Live.GadgetSDK.RssGadget = function(p_elSource, p_args, p_namespace) {
    // always call initializeBase before anything else!
	Microsoft.Live.GadgetSDK.RssGadget.initializeBase(this, arguments);

    // setup private member variables
	var m_this = this;
	var m_el = p_elSource;
	var m_module = p_args.module;
	var m_onDashboard = p_args.onDashboard;
	var m_feedUrl = "http://spaces.msn.com/livecom/feed.rss";
	var m_numItems = p_args.numItems ? p_args.numItems : 5;
	var m_feed = p_args.feed;

	var m_moduleEl;
	var m_isLoaded;
	var m_headlinesEl;

	/****************************************
	**          initialize Method
	****************************************/
    // initialize is always called immediately after your object is instantiated
	this.initialize = function(p_objScope)
	{
	    // always call the base object's initialize first!	
		Microsoft.Live.GadgetSDK.RssGadget.getBaseMethod(this, "initialize", "Web.Bindings.Base").call(this, p_objScope);

        //TODO: perform any initialization/setup work
        GetFeed();
	
	};
	Microsoft.Live.GadgetSDK.RssGadget.registerBaseMethod(this, "initialize");
	
	
	/****************************************	
	**           dispose Method
	****************************************/
	this.dispose = function(p_blnUnload) {
	    //TODO: add your dispose code here
	    
	    // null out all member variables
	    m_this = null;
	    m_el = null;
	    m_module = null;  
	    
        m_onDashboard = null;
	    m_feedUrl = null;

	    m_moduleEl = null;
	    m_isLoaded = null;
	    m_headlinesEl = null;
	    
        // always call the base object's dispose last!	
		Microsoft.Live.GadgetSDK.RssGadget.getBaseMethod(this, "dispose", "Web.Bindings.Base").call(this, p_blnUnload);
	};
	Microsoft.Live.GadgetSDK.RssGadget.registerBaseMethod(this, "dispose");

	/****************************************
	** Other Methods
	****************************************/
		//----------------------------------------------------------------------------
	//	
	//	Method:		GetFeed()
	//
	//	Synopsis:	Fetches RSS feed and displays headlines
	//
	//	Arguments:	none
	//
	//	Returns:	nothing
	//
	//----------------------------------------------------------------------------

	function GetFeed()
	{
		// Create the network request object to fetch the RSS feed via the RSS proxy
		var r = Web.Network.createRequest(
			Web.Network.Type.XML,
			m_feedUrl,
			{proxy:"rss", numItems:m_numItems},
			OnFeedReceived);

		r.execute();
	}
	
	//----------------------------------------------------------------------------
	//
	//	Method:		OnFeedReceived()
	//
	//	Synopsis:	Callback handler for fetching of rss data
	//
	//	Arguments:	response			- xml response
	//
	//	Returns:	nothing
	//
	//----------------------------------------------------------------------------

	function OnFeedReceived(response)
	{
		m_feed = Start.Parser.ParseRssResponse(response);

		if (m_feed)
			RenderFeed();
		else if (!m_isLoaded)
			m_el.innerHTML = "Error";
	}
	
	//----------------------------------------------------------------------------
	//
	//	Method:		RenderFeed()
	//
	//	Synopsis:	Displays feed
	//
	//	Arguments:	none
	//
	//	Returns:	nothing
	//
	//----------------------------------------------------------------------------

	function RenderFeed()
	{
		if (m_feed && m_feed.channels && m_feed.channels.length > 0)
		{
			RenderChannel(m_feed.channels[0]);
		}		
	}
	
	//----------------------------------------------------------------------------
	//
	//	Method:		Start.RssModule.RenderChannel
	//
	//	Synopsis:	Renders results of RSS response
	//
	//	Arguments:	channel				- rss channel
	//
	//	Returns:	nothing
	//
	//----------------------------------------------------------------------------

	function RenderChannel(channel)
	{

		// Prepare container
		if (!m_isLoaded)
		{
			
			if (m_el.innerText == "Loading...")
				m_el.innerText = "";

			m_isLoaded = true;
		}
		else
		{
			ClearFeed();
		}

		// Add headlines
		if (channel.items.length > 0)
		{

			RenderHeadlines(channel);

		}
	}
	
	//----------------------------------------------------------------------------
	//
	//	Method:		RenderHeadlines()
	//
	//	Synopsis:	Renders headline items of RSS response
	//
	//	Arguments:	channel				- rss channel
	//
	//	Returns:	nothing
	//
	//----------------------------------------------------------------------------

	function RenderHeadlines(channel)
	{
		if (!m_headlinesEl)
		{
			m_headlinesEl = document.createElement("ul");
			m_headlinesEl.className = "rssList";
			m_el.appendChild(m_headlinesEl);
		}

		for (var i = 0; i < m_numItems; i++)
		{
			var li = document.createElement("li");
			li.className = "rssItem";
			m_headlinesEl.appendChild(li);
			
			
			var aEl = document.createElement("a");
			li.appendChild(aEl);
			
			aEl.href = channel.items[i].link;
			aEl.target = "top";
			aEl.innerHTML = channel.items[i].title;
		
		}
	}


};
Microsoft.Live.GadgetSDK.RssGadget.registerClass("Microsoft.Live.GadgetSDK.RssGadget", "Web.Bindings.Base");
